<?php

namespace app\common\library\aliyuns;

use AlibabaCloud\SDK\Mts\V20140618\Models\QuerySnapshotJobListRequest;
use AlibabaCloud\SDK\Mts\V20140618\Models\SearchPipelineRequest;
use AlibabaCloud\SDK\Mts\V20140618\Mts;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Mts\V20140618\Models\SubmitSnapshotJobRequest;

class MtsClient extends Base
{

    protected $mtsClient;

    //默认管道
    protected $pipelineId = "2b6f7680df374d468965f234fb91d0c5";
//    protected $pipelineId = "7e173345999b470e9cdd9fb04837a9bb";

    //H264-MP4-准高清1280
    protected $templateId = "S00000001-200030";

    public function __construct()
    {
        parent::__construct();

        $config = new Config([]);
        $config->accessKeyId = $this->accessKey;
        $config->accessKeySecret = $this->accessSecret;
        $config->regionId = $this->regionId;
        $config->endpoint = 'mts.' . $this->regionId . '.aliyuncs.com';

        $this->mtsClient = new Mts($config);
    }

    /**
     * 使用AK&SK初始化账号Client
     * @return Mts Client
     */
    public function createClient()
    {
        $config = new Config([]);
        $config->accessKeyId = $this->accessKey;
        $config->accessKeySecret = $this->accessSecret;
        $config->regionId = $this->regionId;
        $config->endpoint = 'mts.' . $this->regionId . '.aliyuncs.com';
        return new Mts($config);
    }

    /**
     * 视频截图
     * @param string $videoSrcPath 源视频地址
     * @param string $output 截图输出地址
     * @param int $time 第几秒进行截图
     */
    public function snapshotJob(string $videoSrcPath, string $output = '', int $time = 1)
    {
        $srcPath = ltrim($videoSrcPath, '/');
        $output = ltrim($output, '/');
        $input = [
            'Location' => $this->location,
            'Bucket' => $this->bucket,
            'Object' => urlencode($srcPath)
        ];
        //具体参数文档看这里  https://help.aliyun.com/document_detail/29253.html
        $snapshotConfig = [
            'OutputFile' => [
                'Location' => $this->location,
                'Bucket' => $this->bucket,
                'Object' => urlencode($output)
            ],
            "FrameType" => "intra",
            "BlackLevel" => 100,
            'Time' => $time,
        ];
        $request = new SubmitSnapshotJobRequest([
            "input" => json_encode($input),
            "snapshotConfig" => json_encode($snapshotConfig),
            "pipelineId" => $this->pipelineId,
        ]);
        $response = $this->mtsClient->submitSnapshotJob($request);
        return $response->body->toMap();
    }

    /**
     * 视频截图
     * @param string $videoSrcPath 源视频地址
     * @param string $output 截图输出地址, 文件名必须包含 {Count} 这个占位符花括号也是在里面的，截图文件名会替换成从 1 开始的数字
     */
    public function snapshotJobAsync(string $videoSrcPath, string $output, int $time = 0, $num = 20, $interval = 1)
    {
        $srcPath = ltrim($videoSrcPath, '/');
        $output = ltrim($output, '/');
        $input = [
            'Location' => $this->location,
            'Bucket' => $this->bucket,
            'Object' => urlencode($srcPath)
        ];
        //具体参数文档看这里  https://help.aliyun.com/document_detail/29253.html
        $snapshotConfig = [
            'OutputFile' => [
                'Location' => $this->location,
                'Bucket' => $this->bucket,
                'Object' => urlencode($output)
            ],
            "FrameType" => "intra",
            "BlackLevel" => 100,
            'Time' => $time,//截图开始时间。
            'Num' => $num,//异步单张截图：Num=1。代表在指定Time时间点开始截取1张。 >1就是多张
            'Interval' => $interval, //截图间隔时间
        ];
        $request = new SubmitSnapshotJobRequest([
            "input" => json_encode($input),
            "snapshotConfig" => json_encode($snapshotConfig),
            "pipelineId" => $this->pipelineId,
        ]);
        $response = $this->mtsClient->submitSnapshotJob($request);
        return $response->body->toMap();
    }

    /**
     * 查询截图任务结果
     * @param string $jobIds 可以是多个，用逗号隔开
     * @return array
     */
    public function querySnapshotJob(string $jobIds): array
    {
        $request = new QuerySnapshotJobListRequest([
            "snapshotJobIds" => $jobIds,
        ]);
        $runtime = new RuntimeOptions([]);
        $response = $this->mtsClient->querySnapshotJobListWithOptions($request, $runtime);
        return $response->body->toMap();
    }

    /**
     * 查询所有管道
     * @return array
     */
    public function getPipelineList(): array
    {
        $request = new SearchPipelineRequest();
        $response = $this->mtsClient->searchPipeline($request);
        return $response->body->toMap();
    }

    /**
     * 通过管道ID查询管道信息
     * @param string $pipelineId 管道ID，可以用逗号隔开
     * @return array
     */
    public function getPipelineById(string $pipelineId): array
    {
        $request = new SearchPipelineRequest();
        $request->pipelineIds = $pipelineId;
        $response = $this->mtsClient->searchPipeline($request);
        return $response->body->toMap();
    }

}